package com.wanmait.employment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.employment.entity.*;
import com.wanmait.employment.mapper.CompanyJobHoursBasiceMapper;
import com.wanmait.employment.mapper.CompanyMapper;
import com.wanmait.employment.mapper.JobHoursBasiceMapper;
import com.wanmait.employment.service.CompanyJobHoursBasiceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CompanyJobHoursBasiceServiceImpl implements CompanyJobHoursBasiceService {
    @Resource
    private CompanyJobHoursBasiceMapper companyJobHoursBasiceMapper;

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private JobHoursBasiceMapper jobHoursBasiceMapper;


    @Override
    public PageInfo<CompanyJobHoursBasice> selectList(String title, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<CompanyJobHoursBasice> companyJobHoursBasiceQueryWrapper=new QueryWrapper<>();
        companyJobHoursBasiceQueryWrapper.inSql("company_id","SELECT id FROM company where company_name like '%"+title+"%'");
        List<CompanyJobHoursBasice> companyJobHoursBasiceList=companyJobHoursBasiceMapper.selectList(companyJobHoursBasiceQueryWrapper);
        PageInfo<CompanyJobHoursBasice> pageInfo=new PageInfo<>(companyJobHoursBasiceList);
        companyJobHoursBasiceList.forEach(companyJobHoursBasice -> {

            QueryWrapper<Company> companyQueryWrapper=new QueryWrapper<>();
            companyQueryWrapper.eq("id",companyJobHoursBasice.getCompanyId());
            Company company=companyMapper.selectOne(companyQueryWrapper);
            companyJobHoursBasice.setCompany(company);

            QueryWrapper<JobHoursBasice> jobHoursBasiceQueryWrapper=new QueryWrapper<>();
            jobHoursBasiceQueryWrapper.eq("id",companyJobHoursBasice.getJobHoursBasiceId());
            JobHoursBasice jobHoursBasice=jobHoursBasiceMapper.selectOne(jobHoursBasiceQueryWrapper);
            companyJobHoursBasice.setJobHoursBasice(jobHoursBasice);
        });
        return pageInfo;
    }

//        QueryWrapper<Company> companyQueryWrapper1 =new QueryWrapper<>();
//        companyQueryWrapper1.inSql("company_id","SELECT id FROM Company WHERE company_Name like '%"+title+"%'").or().inSql("job_hours_basice_id","SELECT id FROM job_hours_basice WHERE work_hours like '%"+title+"%'");
//        List<Company> companyList = companyMapper.selectList(companyQueryWrapper1);
//        ArrayList<Integer> companyIds = new ArrayList<>();

//        companyList.forEach(company -> {
//            companyIds.add(company.getId());
//        });
//        QueryWrapper<JobHoursBasice> jobHoursBasiceQueryWrapper = new QueryWrapper<>();
//        jobHoursBasiceQueryWrapper.like("title",title);
//        List<JobHoursBasice> jobHoursBasiceList = jobHoursBasiceMapper.selectList(jobHoursBasiceQueryWrapper);
//        ArrayList<Integer> jobHoursBasiceIds = new ArrayList<>();
//        jobHoursBasiceList.forEach(jobHoursBasice -> {
//            jobHoursBasiceIds.add(jobHoursBasice.getId());
//        });
//        QueryWrapper<CompanyJobHoursBasice> queryWrapper=new QueryWrapper<>();
//        queryWrapper.in(!companyIds.isEmpty(),"company_id",companyIds);
//        queryWrapper.in(!jobHoursBasiceList.isEmpty(),"job_hours_basice_id",jobHoursBasiceIds);
//        System.out.println(title);
//
//        PageHelper.startPage(pageNum,pageSize);
//        List<CompanyJobHoursBasice> companyJobHoursBasiceList=companyJobHoursBasiceMapper.selectList(queryWrapper);
//        PageInfo<CompanyJobHoursBasice> companyJobHoursBasicePageInfo=new PageInfo<>(companyJobHoursBasiceList);
//        companyJobHoursBasicePageInfo.getList().forEach((companyJobHoursBasice) -> {
//
//            QueryWrapper<Company> companyQueryWrapper=new QueryWrapper<>();
//            companyQueryWrapper.eq("id",companyJobHoursBasice.getCompanyId());
//            Company company=companyMapper.selectOne(companyQueryWrapper);
//            companyJobHoursBasice.setCompany(company);
//
//            QueryWrapper<JobHoursBasice> jobHoursBasiceQueryWrapper1=new QueryWrapper<>();
//            jobHoursBasiceQueryWrapper1.eq("id",companyJobHoursBasice.getJobHoursBasiceId());
//            JobHoursBasice jobHoursBasice =jobHoursBasiceMapper.selectOne(jobHoursBasiceQueryWrapper1);
//            companyJobHoursBasice.setJobHoursBasice(jobHoursBasice);
//        });


    @Override
    public Integer update(CompanyJobHoursBasice companyJobHoursBasice) {
        return companyJobHoursBasiceMapper.updateById(companyJobHoursBasice);
    }

    @Override
    public Integer insert(CompanyJobHoursBasice companyJobHoursBasice) {
        return companyJobHoursBasiceMapper.insert(companyJobHoursBasice);
    }

    @Override
    public void delete(Integer id) {
        companyJobHoursBasiceMapper.deleteById(id);

    }
}
